﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sengoku
{
    //this is where the action is. gameplay and victory conditions and whatnot. this class makes for a game as described in the original assignment with a 6 by 6 tile board.
    class BasicGame
    {
        private Board board = new Board(6, 6);

        public void populate()
        {
            board.addPieceAt(new BetaPiece(0), 1, 1);
            board.addPieceAt(new AlphaPiece(0), 1, 2);
            board.addPieceAt(new AlphaPiece(0), 1, 3);
            board.addPieceAt(new BetaPiece(0), 1, 4);

            board.addPieceAt(new BetaPiece(1), 4, 1);
            board.addPieceAt(new AlphaPiece(1), 4, 2);
            board.addPieceAt(new AlphaPiece(1), 4, 3);
            board.addPieceAt(new BetaPiece(1), 4, 4);

            printBoard();
        }

        public void playGame()
        {
            bool uppercaseWin = false;
            bool lowercaseWin = false;
            int turnCounter = 200;

            while (!uppercaseWin && !lowercaseWin && turnCounter > 0)
            {
                turnCounter--;

                uppercaseTurn();
                printBoard();
                uppercaseWin = searchForAlpha(1);

                lowercaseTurn();
                printBoard();
                lowercaseWin = searchForAlpha(0);
            }

            if (uppercaseWin)
                Console.WriteLine("**Player One (uppercase) Wins!**");
            else if (lowercaseWin)
                Console.WriteLine("**Player Two (lowercase) Wins!**");
            else
                if(board.numberOfUppercase() > board.numberOfLowercase())
                    Console.WriteLine("**Player One (uppercase) Wins!**");
                else
                    Console.WriteLine("**Player Two (lowercase) Wins!**");
        }

        public void uppercaseTurn()
        {
            bool turnOver = false;
            Console.WriteLine();
            Console.WriteLine("Player One (uppercase), choose the coordinates of a piece to move.");
            while (!turnOver)
            {
                Console.Write("Column:  ");
                int c = Int32.Parse(Console.ReadLine());
                Console.Write("Row: ");
                int r = Int32.Parse(Console.ReadLine());
                Position pos = new Position(c, r);

                if ((board.getPieceAt(pos.getC(), pos.getR()).getCase()) == 1)
                {
                    Position newPos = (board.getPieceAt(pos.getC(), pos.getR())).move();
                    board.addPieceAt((board.getPieceAt(pos.getC(), pos.getR())), newPos.getC(), newPos.getR());
                    board.removePieceAt(pos.getC(), pos.getR());
                    turnOver = true;
                }
                else
                    Console.WriteLine("Invalid");
            }
        }

        public void lowercaseTurn()
        {
            bool turnOver = false;
            Console.WriteLine();
            Console.WriteLine("Player Two (lowercase), choose the coordinates of a piece to move.");
            while (!turnOver)
            {
                Console.Write("Column:  ");
                int c = Int32.Parse(Console.ReadLine());
                Console.Write("Row: ");
                int r = Int32.Parse(Console.ReadLine());
                Position pos = new Position(c, r);

                if ((board.getPieceAt(pos.getC(), pos.getR()).getCase()) == 0)
                {
                    Position newPos = (board.getPieceAt(pos.getC(), pos.getR())).move();
                    board.addPieceAt((board.getPieceAt(pos.getC(), pos.getR())), newPos.getC(), newPos.getR());
                    board.removePieceAt(pos.getC(), pos.getR());
                    turnOver = true;
                }
                else
                    Console.WriteLine("Invalid");
            }
        }

        public bool searchForAlpha(int c)
        {
            int alphaCount = 0;
            for (int i = 0; i < board.getColumns(); i++)
                for (int j = 0; j < board.getRows(); j++)
                    if (board.getPieceAt(i, j) != null && ((board.getPieceAt(i, j)).getIsAlpha() == true && (board.getPieceAt(i, j)).getCase() != c)) //increments alphaCount for each of the oppenent's alphas.
                        alphaCount++;
            if (alphaCount == 0) //if no enemy alphas are found, the player making the check is the winner.
                return true;
            else
                return false;
        }

        public void printBoard()
        {
            Console.WriteLine();
            Console.Write("  ");
            for (int j = 0; j < board.getRows(); j++)
                Console.Write(j + "    ");
            Console.WriteLine();

            for (int i = 0; i < board.getColumns(); i++)
            {
                Console.Write(i + " ");
                for (int j = 0; j < board.getRows(); j++)
                {
                    if (board.getPieceAt(i, j) != null)
                    {
                        Console.Write((board.getPieceAt(i, j)).getLetter() + "    ");
                    }
                    else
                    {
                        Console.Write("X    ");
                    }
                }
                Console.WriteLine();
            }
        }
    }
}
